package com.bdqn.struts.action;

import com.opensymphony.xwork2.ActionSupport;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class UploadAction2 extends ActionSupport {
    //封装文件标题属性
    private String title;
    //获取提交的多个文件
    private File[] upload;
    //获取提交的文件类型
    private String[] uploadContentType;
    //封装文件上传名称
    private String[] uploadFileName;
    //获取文件上传的路径
    private String savePath;

    @Override
    public String execute() throws Exception {
        byte[] buffer=new byte[1024];
        for (int i=0;i< upload.length;i++){
            FileInputStream fis=new FileInputStream(getUpload()[i]);
            FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.uploadFileName[i]);
            int length=fis.read(buffer);
            while (length>0){
                //每次写入length长度的内容
                fos.write(buffer,0,length);
                length=fis.read(buffer);
            }
            fis.close();
            fos.flush();
            fos.close();
            return SUCCESS;

        }
        return super.execute();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public File[] getUpload() {
        return upload;
    }

    public void setUpload(File[] upload) {
        this.upload = upload;
    }

    public String[] getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String[] uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String[] getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String[] uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
}
